import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.sql.*;

import javax.sql.*;

public class Warning
{
    public Warning()
    {
        super();
    }
    
    void connect ( String portName ) throws Exception
    {
       CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();
                
                (new Thread(new SerialReader(in,out))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    
    /** */
    public static class SerialReader implements Runnable 
    {
        InputStream in;
        OutputStream out;

      	 Connection conn = null; // connection object
      	    Statement stmt = null; // statement object
      	    ResultSet rs = null; // result set object
        
        public SerialReader ( InputStream in,OutputStream out )
        {
            this.in = in;
            this.out = out;
        }
        
        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            String s=null;
            int ss=0;
            try
            	{

            	try{
            		Class.forName("com.mysql.jdbc.Driver").newInstance();
            	
		    // define database connection parameters
	    	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","root");
	    	
	      stmt = conn.createStatement();
	      // start a transaction
	      conn.setAutoCommit(true);
	      
	      String selectQuery="select * from studentA where start_date='";
	      
	     ResultSet rs= stmt.executeQuery(selectQuery);
	     
	     while(rs.next())
	     {
	    	  s=rs.getString("student");
	     ss=rs.getInt("attendance");
	    	 int ssid=rs.getInt(1);
	    	 
	    	 System.out.println("---------------------------"+s);
	    	 System.out.println("---------------------------"+ss);
	     }	} catch (Exception e){
            		
            		System.out.println(e);
            		
            	}
				
		    
             		try {
                 	    Thread.sleep(6000);
                 	} catch(InterruptedException ex) {
                 	    Thread.currentThread().interrupt();
                 	}
     		   this.out.write('A');
               this.out.write('T');
               this.out.write('+');
               this.out.write('C');
               this.out.write('M');
               this.out.write('G');
               this.out.write('S');
               this.out.write('=');
               this.out.write('"');
               this.out.write('+');
               this.out.write('9');
               this.out.write('1');
               this.out.write('7');
               this.out.write('4');
               this.out.write('1');
               this.out.write('8');
               this.out.write('8');
               this.out.write('6');
               this.out.write('7');
               this.out.write('1');
               this.out.write('9');
               this.out.write('8');
               this.out.write('"');
               this.out.write(0X0a);
               this.out.write(0X0D);

               this.out.write('A');
               this.out.write('L');
               this.out.write('E');
               this.out.write('R');
               this.out.write('T');
               this.out.write('!');
               this.out.write('!');
               this.out.write('!');
               this.out.write(26);
                //}
               
            	}catch (Exception e){
            		
            		e.printStackTrace();

            	}	
            	}
        
              
        }
    

 public static void main ( String[] args ) throws SQLException
    {
	 Connection conn = null; // connection object
	    Statement stmt = null; // statement object
	    ResultSet rs = null; // result set object
	    try {
	    	Class.forName("oracle.jdbc.driver.OracleDriver");
		    // define database connection parameters
	    	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","root");
		    
	  //    conn = getConnection(); // without Connection, can not do much
	      // create a statement: This object will be used for executing
	      // a static SQL statement and returning the results it produces.
	      stmt = conn.createStatement();
	      // start a transaction
	      conn.setAutoCommit(false);

	      // insert two new records to the cats_tricks table
	     // stmt.executeUpdate("INSERT INTO cats_tricks VALUES('mono', 'r')");

	      // commit the transaction
	      conn.commit();

	      // set auto commit to true (from now on every single
	      // statement will be treated as a single transaction
	      conn.setAutoCommit(true);

	      // get all of the the records from the cats_tricks table
	      rs = stmt.executeQuery("SELECT name, trick FROM cats_tricks");

	      // iterate the result set and get one row at a time
	      while (rs.next()) {
	        String name = rs.getString(1); // 1st column in query
	        String trick = rs.getString(2); // 2nd column in query
	        System.out.println("name=" + name);
	        System.out.println("trick=" + trick);
	        System.out.println("==========");
	      }
	    } catch (ClassNotFoundException ce) {
	      // if the driver class not found, then we will be here
	      System.out.println(ce.getMessage());
	    } catch (SQLException e) {
	      // something went wrong, we are handling the exception here
	      if (conn != null) {
	        conn.rollback();
	        conn.setAutoCommit(true);
	      }
	    

	      System.out.println("--- SQLException caught ---");
	      // iterate and get all of the errors as much as possible.
	      while (e != null) {
	        System.out.println("Message   : " + e.getMessage());
	        System.out.println("SQLState  : " + e.getSQLState());
	        System.out.println("ErrorCode : " + e.getErrorCode());
	        System.out.println("---");
	        e = e.getNextException();
	      }
	    } finally { // close db resources
	      try {
	        rs.close();
	        stmt.close();
	        conn.close();
	      } catch (Exception e) {
	      }

	    }
        try
        {
            (new Warning()).connect("COM46");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}